/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbm.smsbroadcast;

import com.elcom.emocbc.cbm.BroadcastDTO;
import com.elcom.emocbc.dbmapping.persistence.DbAccess;
import com.elcom.emocbc.util.ConfigurationManager;
import java.io.File;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
public class SmsBroadcastMain {
    
    private final Logger logger = LoggerFactory.getLogger(SmsBroadcastMain.class);
    
    public void init(String[] args) {
        try {
            PropertyConfigurator.configureAndWatch(System.getProperty("user.home") + File.separator + "config/cellbroadcast" + File.separator + "log4j-smsbroadcast.properties");
            setupDatabaseAccess();
            Thread.sleep(5000L);
            
            Integer zoneId = Integer.valueOf(args[1]);
            String startTime = args[2];
            String[] msisdnList = args[3].split(",");
            
            BroadcastDTO broadcast = new SmsBroadcastDao().getBroadcast(startTime, zoneId);
            SmsBroadcastJob smsBroadcastJob = new SmsBroadcastJob(broadcast, msisdnList);
            new Thread(smsBroadcastJob).start();
        } catch (InterruptedException ex) {
            logger.error("", ex);
        }
    }
    
    private void setupDatabaseAccess() {
        ConfigurationManager.reloadConfiguration();
        Properties properties = new Properties();
        properties.setProperty("url", ConfigurationManager.getStrProp("cell_broadcast", "URL", "jdbc:oracle:thin:@192.168.6.249:1521:mca"));
        properties.setProperty("classDriverName", ConfigurationManager.getStrProp("cell_broadcast", "DRIVER_CLASS_NAME", "oracle.jdbc.driver.OracleDriver"));
        properties.setProperty("user", ConfigurationManager.getStrProp("cell_broadcast", "USER_NAME", "livescreen"));
        properties.setProperty("password", ConfigurationManager.getStrProp("cell_broadcast", "PASSWORD", "livescreen"));
        
        DbAccess.getInstance().setProperties(properties);
        logger.info("Done setup Database access");
    }
}
